function initTimeType(id) {
    $("#"+id).change(function () {//时间类型的切换
        var _this = $(this);
        var type = _this.val();
        if(type==1||type==5){//今日实时
            _this.next('.time-box').addClass('hide');
            _this.next('.time-box').next('.time-input-box').addClass('hide');
        }else if(type==2 || type==3){//近7天、近30天
            _this.next('.time-box').removeClass('hide');
            _this.next('.time-box').next('.time-input-box').addClass('hide');
            var startDate = new Date();
            if(type==2){//7天
                startDate.setDate(startDate.getDate() -7);
            }else{//30天
                startDate.setDate(startDate.getDate() -30);
            }
            _this.next(".time-box").find(".start-time").text(startDate.getFullYear()+"-"+(startDate.getMonth()+1)+"-"+startDate.getDate());
            var endDate = new Date();
            endDate.setDate(endDate.getDate()-1);
            _this.next(".time-box").find(".end-time").text(endDate.getFullYear()+"-"+(endDate.getMonth()+1)+"-"+endDate.getDate());
        }else if(type==4){//自定义
            _this.next('.time-box').addClass('hide');
            _this.next('.time-box').next('.time-input-box').removeClass('hide');
            var list  =_this.next('.time-box').next('.time-input-box').find("input:text");
            time_event(4,$(list[0]).val(),$(list[1]).val(),id);
        }
        if(type !=4){
            time_event(type,null,null,id);
        }
    });
}

/**
 * 初始化时间控件
 * @param statId 开始ID
 * @param endId 结果ID
 * @param timeTypeId 时间类型selectID
 */
function initTime(statId,endId,timeTypeId) {
    var nowDate = new Date();
    nowDate.setDate(nowDate.getDate() -1);
    //日期时间控件初始化
    var startTime = {
        elem: '#'+statId, //需显示日期的元素选择器
        event: 'click', //触发事件
        format: 'YYYY-MM-DD', //日期格式
        istime: false, //是否开启时间选择
        start: '',    //开始日期
        istoday:false,
        max:(nowDate.getFullYear()+'-'+(nowDate.getMonth()+1)+"-"+nowDate.getDate()),
        choose: function(dates){ //选择好日期的回调
            var obj = new Date(dates);
            obj.setDate(obj.getDate()+365);
            var nowDate = new Date();
            nowDate.setDate(nowDate.getDate() -1);
            if(obj.getTime() > nowDate.getTime()){
                obj = nowDate;
            }
            var maxTDate = obj.getFullYear()+"-"+(obj.getMonth()+1)+"-" + obj.getDate();
            endTime.max = maxTDate;
            endTime.min = dates; //开始日选好后，重置结束日的最小日期
            time_event(4,$("#"+statId).val(),$("#"+endId).val(),timeTypeId);
        }
    }

    var endTime = {
        elem: '#'+endId, //需显示日期的元素选择器
        event: 'click', //触发事件
        format: 'YYYY-MM-DD', //日期格式
        istime: false, //是否开启时间选择
        start: '',    //开始日期
        istoday:false,
        max:(nowDate.getFullYear()+'-'+(nowDate.getMonth()+1)+"-"+nowDate.getDate()),
        choose: function(dates){ //选择好日期的回调
            var obj = new Date(dates);
            obj.setDate(obj.getDate()-365);
            var minTDate = obj.getFullYear()+"-"+(obj.getMonth()+1)+"-" + obj.getDate();
            startTime.min = minTDate;
            startTime.max = dates; //结束日选好后，重置开始日的最大日期
            time_event(4,$("#"+statId).val(),$("#"+endId).val(),timeTypeId);
        }
    }
    laydate(startTime);
    laydate(endTime);
}

/**
 * 时间事件
 * @param type 1=今日实时、2=近7天、3=近30天、4=自定义时间
 * @param startTime
 * @param endTime
 * @param timeTypeId 时间类型selectId
 */
function time_event(type,startTime,endTime,timeTypeId) {
    if(type==4){
        var startDate = new Date(startTime);
        var endDate = new Date(endTime);
        var dValueTime = endDate.getTime() - startDate.getTime();
        var dValueDay = parseInt(dValueTime / 1000 /60/60/24);
        dValueDay = Math.abs(dValueDay);
        if((dValueDay+1) > 366){
            icModale(1,'自定义时间最多可设置366天之间的时间')
            return;
        }
    }
    if(type==4){
        if(startTime==null || $.trim(startTime)=='' || endTime==null || $.trim(endTime)==''){
            return;
        }

        var _startTime = parseInt(startTime.replace(/-/g,""));
        var _endTime = parseInt(endTime.replace(/-/g,""));
        if(_startTime > _endTime){
            return;
        }
    }
    if(timeTypeId!=null&&timeTypeId!=""){
        var em = document.getElementById(timeTypeId);
        var statisticsTime = new CustomEvent('statistics-time',{
            detail : {
                'type':type,
                'startTime':startTime,
                'endTime':endTime
            }
        });
        em.dispatchEvent(statisticsTime);
    }
}